package com.example.myapplication

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 获取控件
        val weightInput: EditText = findViewById(R.id.weight_input)
        val heightInput: EditText = findViewById(R.id.height_input)
        val calculateButton: Button = findViewById(R.id.calculate_button)
        val resultText: TextView = findViewById(R.id.result_text)

        // 设置按钮点击事件
        calculateButton.setOnClickListener {
            // 获取用户输入的体重和身高
            val weight = weightInput.text.toString().toFloatOrNull()
            val height = heightInput.text.toString().toFloatOrNull()

            if (weight != null && height != null && height > 0) {
                // 计算 BMI
                val bmi = weight / (height * height)

                // 显示结果
                resultText.text = "您的 BMI: %.2f\n%s".format(bmi, getHealthAdvice(bmi))
                resultText.visibility = View.VISIBLE
            } else {
                resultText.text = "请输入有效的体重和身高"
                resultText.visibility = View.VISIBLE
            }
        }
    }

   
    private fun getHealthAdvice(bmi: Float): String {
        return when {
            bmi < 18.5 -> "您的 BMI 值较低"
            bmi in 18.5..24.9 -> "您的 BMI 值正常"
            bmi in 25.0..29.9 -> "您的 BMI 值较高"
            else -> "您的 BMI 值非常高"
        }
    }
}